package com.base.sms.handler.aixun;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.base.core.network.SimpleHttpUtils;
import com.base.core.network.http.ResponseEntity;
import com.base.core.placeholder.PlaceholderResolver;
import com.base.sms.config.AixunConfig;
import com.base.sms.model.enums.SMSHandlerEnum;
import com.base.sms.model.query.NoticeQuery;
import com.base.sms.exception.SendFailedException;
import com.base.sms.handler.SendHandler;
import com.base.sms.handler.aliyun.AliyunSendHandler;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.stream.Collectors;

@Component
public class AixunSendHandler implements SendHandler {

    private final static Log log = LogFactory.get(AliyunSendHandler.class);

    private static final String OK = "OK";

    private final AixunConfig properties;

    private final ObjectMapper objectMapper;


    /**
     * 构造阿里云短信发送处理
     *
     * @param properties   阿里云短信配置
     * @param objectMapper objectMapper
     */
    @Autowired
    public AixunSendHandler(AixunConfig properties, ObjectMapper objectMapper) {
        this.properties = properties;
        this.objectMapper = objectMapper;
    }

    /**
     * 发送通知
     *
     * @param noticeQuery 通知内容
     * @param phones     手机号列表
     * @return 是否发送成功
     */
    @Override
    public boolean send(NoticeQuery noticeQuery, Collection<String> phones) {
        try {
            String url = properties.getUrl();
            if(ObjectUtil.isEmpty(url)){
                throw new SendFailedException("缺少请求url参数");
            }
            noticeQuery.getParams().put("username", properties.getUsername());
            noticeQuery.getParams().put("password", properties.getPassword());
            noticeQuery.getParams().put("mobile", phones.stream().collect(Collectors.joining(",")));
            if(ObjectUtil.isNotEmpty(noticeQuery.getParams().get("code"))){
                String code = noticeQuery.getParams().get("code");
                //解析模板占位符
                String template = PlaceholderResolver.getDefaultResolver().resolve(properties.getTemplate(), code);
                String content = properties.getSignName() + template;
                noticeQuery.getParams().put("content", content);
                noticeQuery.getParams().remove("code");
            }
            //String paramString = objectMapper.writeValueAsString(noticeQuery.getParams());
            ResponseEntity responseEntity = SimpleHttpUtils.get(url, noticeQuery.getParams(),false);
            String result = responseEntity.getBody();
            if(Long.valueOf(result).compareTo(Long.valueOf("0")) <= 0){
                return false;
            }
        } catch (Exception e) {
            log.debug(e.getMessage(), e);
            return false;
        }
        return true;
    }

    @Override
    public String support() {
        return SMSHandlerEnum.AIXUN.type();
    }
}
